package xgenerator.ui.swing.adapter;

import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import xgenerator.ui.swing.AboutDialog;
import xgenerator.ui.swing.JGeneratorFrame;
import xgenerator.ui.swing.HelpContentFrame;
import xgenerator.ui.swing.XGeneratorFrame;
import xgenerator.util.HttpClient;

/**
 * <p>
 * Title:菜单、按钮Action
 * </p>
 * <p>
 * Description:具体功能见各方法描述
 * </p>
 * <p>
 * Copyright:Copyright (c) 2012
 * </p>
 * 
 * @author <a href="mailto:lishushan@gmail.com">liss</a>
 * @version 1.0
 */
public class MenuAdapter extends AbstractAction {

	public static final String HOME_COMMAND = "home";
	public static final String METADATA_GENERATOR_COMMAND = "metadataGenerator";
	public static final String QUERY_GENERATOR_COMMAND = "queryGenerator";
	public static final String EXIT_COMMAND = "exit";
	
	public static final String FULL_SCREEN_COMMAND = "fullScreen";
	public static final String NORMAL_SCREEN_COMMAND = "normalScreen";
	
	public static final String PREFERENCES_COMMAND = "preferences";
	
	public static final String HELP_COMMAND = "help";
	public static final String LINK_COMMAND = "link";
	public static final String UPDATE_COMMAND = "update";
	public static final String ABOUT_COMMAND = "about";
	
	/**
	 * UI界面
	 */
	private Component ui;
	
	private JGeneratorFrame jGeneratorFrame;

	/**
	 * constructor
	 * 
	 * @param ui
	 */
	public MenuAdapter(Component ui) {
		this.ui = ui;
	}

	/**
	 * <p>
	 * Title：actionPerformed
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void actionPerformed(ActionEvent e) {
		final String actionCommand = e.getActionCommand();

		if (HOME_COMMAND.equals(actionCommand)) {
			homeActionPerformed(e);
		} else if (METADATA_GENERATOR_COMMAND.equals(actionCommand)) {
			metadataGeneratorActionPerformed(e);
		} else if (QUERY_GENERATOR_COMMAND.equals(actionCommand)) {
			queryGeneratorActionPerformed(e);
		} else if (EXIT_COMMAND.equals(actionCommand)) {
			exitActionPerformed(e);
		} else if (FULL_SCREEN_COMMAND.equals(actionCommand)) {
			fullScreenActionPerformed(e);
		} else if (NORMAL_SCREEN_COMMAND.equals(actionCommand)) {
			normalScreenActionPerformed(e);
		} else if (PREFERENCES_COMMAND.equals(actionCommand)) {
			preferencesActionPerformed(e);
		} else if (HELP_COMMAND.equals(actionCommand)) {
			helpActionPerformed(e);
		} else if (LINK_COMMAND.equals(actionCommand)) {
			linkActionPerformed(e);
		} else if (UPDATE_COMMAND.equals(actionCommand)) {
			updateActionPerformed(e);
		} else if (ABOUT_COMMAND.equals(actionCommand)) {
			aboutActionPerformed(e);
		}
	}

	/**
	 * <p>
	 * Title：表元数据代码生成器菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void metadataGeneratorActionPerformed(ActionEvent e) {
		if (null == jGeneratorFrame) {
			jGeneratorFrame = new JGeneratorFrame();
		}
		if(!jGeneratorFrame.isVisible()) {
			this.jGeneratorFrame.setVisible(true);
		}
		SwingUtilities.updateComponentTreeUI(jGeneratorFrame);
	}

	/**
	 * <p>
	 * Title：pdm代码生成器菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void queryGeneratorActionPerformed(ActionEvent e) {
		System.out.println("queryGeneratorActionPerformed");
	}

	/**
	 * <p>
	 * Title：存储过程代码生成器菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void homeActionPerformed(ActionEvent e) {
		System.out.println("homeActionPerformed");
	}

	/**
	 * <p>
	 * Title：退出菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void exitActionPerformed(ActionEvent e) {
		if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(ui, "确认退出？", "确认", JOptionPane.YES_NO_OPTION)) {
			System.exit(0);
		}
	}
	
	/**
	 * <p>
	 * Title：全屏菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void fullScreenActionPerformed(ActionEvent e) {
		XGeneratorFrame xgeneratorUI = (XGeneratorFrame) ui;
		xgeneratorUI.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
		xgeneratorUI.setFullScreen(true);
	}
	
	/**
	 * <p>
	 * Title：正常屏菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void normalScreenActionPerformed(ActionEvent e) {
		XGeneratorFrame xgeneratorUI = (XGeneratorFrame) ui;
		xgeneratorUI.setExtendedState(java.awt.Frame.NORMAL);
		xgeneratorUI.setFullScreen(false);
	}

	/**
	 * <p>
	 * Title：设置菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void preferencesActionPerformed(ActionEvent e) {
		System.out.println("preferencesActionPerformed");
	}

	/**
	 * <p>
	 * Title：帮助菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void helpActionPerformed(ActionEvent e) {
		new HelpContentFrame();
	}
	
	/**
	 * <p>
	 * Title：链接菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void linkActionPerformed(ActionEvent e) {
		try {
			Runtime.getRuntime().exec("cmd.exe /c start " + "http://liss.iteye.com/");
			//上面的Runtime语句可用此句代替Runtime.getRuntime().exec("explorer+ "http://liss.iteye.com/");
			/*
			 * jdk6
			Desktop desktop = Desktop.getDesktop();
			desktop.browse(new URI("URL地址"));
		    */
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(ui, ex.getMessage());
		}
	}

	/**
	 * <p>
	 * Title：更新菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void updateActionPerformed(ActionEvent e) {
		try {
			HttpClient httpClient = new HttpClient();
			String requestUrl = "http://liss.iteye.com/";
			String responseEncoding = "UTF-8";
			//System.out.println(httpClient.doGet(requestUrl, responseEncoding));
			if(true) {
				JOptionPane.showMessageDialog(ui, "恭喜！\n你的XGenerator已经是最新版本。", "XGenerator在线升级", JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(ui, "你将升级到2.0版本，新增功能和优化点如下：", "XGenerator在线升级", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(ui, ex.getMessage(), "XGenerator在线升级", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	/**
	 * <p>
	 * Title：about菜单项
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void aboutActionPerformed(ActionEvent e) {
		new AboutDialog((Frame) ui, "关于XGenerator", true);
	}

}
